package com.problem.leetcode;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

/**
 * @author didi
 * @DESCRIPTION
 * @create 2019-05-27 13:32
 **/
@State(Scope.Thread)
@BenchmarkMode({Mode.Throughput})
@Warmup(iterations = 3, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 5, timeUnit = TimeUnit.MICROSECONDS)
@Threads(16)
@Fork(4)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class Problem461 {

    public int hammingDistance(int x, int y) {
        int num = x ^ y;
        //之后就转变成求一个数 二进制中1的个数
        int count = 0;
        while (num > 0) {
            num &= (num - 1);
            ++count;
        }
        return count;
    }

    @Benchmark
    public void testHaming() {
        int x = Integer.MAX_VALUE;
        int y = 1289786;
        hammingDistance(x, y);
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder().include(Problem461.class.getSimpleName()).build();
        new Runner(opt).run();
    }
}
